let p = [1, 2, 3], q = [1, 2, 3]
function TreeNode(val, left, right) {
    this.val = (val === undefined ? 0 : val);
    this.left = (left === undefined ? null : left);
    this.right = (right === undefined ? null : right);
}
function arrayToTree(arr) {
    if (arr.length === 0 || arr[0] === null) return null;
    const root = new TreeNode(arr[0]);
    const queue = [root];
    let i = 1;
    while (queue.length > 0 && i < arr.length) {
        const current = queue.shift();

        // 处理左子节点
        if (i < arr.length && arr[i] !== null) {
            current.left = new TreeNode(arr[i]);
            queue.push(current.left);
        }
        i++;

        // 处理右子节点
        if (i < arr.length && arr[i] !== null) {
            current.right = new TreeNode(arr[i]);
            queue.push(current.right);
        }
        i++;
    }

    return root;
}
var isSameTree = function (p, q) {
    // 如果两个节点都为空，返回 true
    if (!p && !q) return true;
    // 如果一个节点为空，另一个不为空，返回 false
    if (!p || !q) return false;
    // 如果两个节点的值不相等，返回 false
    if (p.val !== q.val) return false;

    let queue = [p];
    let que = [q];
    while (queue.length > 0) {
        const current = queue.shift();
        const cur = que.shift();

        // 比较左子节点
        if (current.left && cur.left) {
            if (current.left.val !== cur.left.val) {
                return false;
            }
            que.push(cur.left);
            queue.push(current.left);
        } else if (current.left || cur.left) {
            // 一个有左子节点，另一个没有，返回 false
            return false;
        }

        // 比较右子节点
        if (current.right && cur.right) {
            if (current.right.val !== cur.right.val) {
                return false;
            }
            que.push(cur.right);
            queue.push(current.right);
        } else if (current.right || cur.right) {
            // 一个有右子节点，另一个没有，返回 false
            return false;
        }
    }
    return true;
};


console.log(isSameTree(arrayToTree(p), arrayToTree(q)));
